cmake_minimum_required(VERSION 3.16)  
project(camera_driver)

# 设置C++标准（ROS2推荐C++17）
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 编译选项（严格模式）
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)  
endif()

# 导出compile_commands.json（方便IDE索引）
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 查找ROS2依赖
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(image_transport REQUIRED)
find_package(camera_info_manager REQUIRED)  # 相机信息管理
find_package(sensor_msgs REQUIRED)  # 图像和相机信息消息


find_library(LIBUSB_LIB usb-1.0 REQUIRED)
find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h HINTS /usr/include/libusb-1.0 REQUIRED)
# --------------------------
# 相机SDK路径配置
# --------------------------
# 架构自动识别（x86_64/arm64等）
set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
message(STATUS "Current architecture: ${ARCH}")

# 海康SDK路径
set(HIKROBOT_SDK_PATH "${CMAKE_CURRENT_SOURCE_DIR}/sdk/hikrobot")
set(HIKROBOT_INCLUDE_DIR "${HIKROBOT_SDK_PATH}/include")
# 根据架构选择库路径
if(ARCH MATCHES "x86_64")
  set(HIKROBOT_LIB_DIR "${HIKROBOT_SDK_PATH}/lib/amd64")
elseif(ARCH MATCHES "aarch64")
  set(HIKROBOT_LIB_DIR "${HIKROBOT_SDK_PATH}/lib/arm64")
else()
  message(FATAL_ERROR "Unsupported architecture for Hikrobot SDK: ${ARCH}")
endif()

# 迈德威视SDK路径
set(MINDVISION_SDK_PATH "${CMAKE_CURRENT_SOURCE_DIR}/sdk/mindvision")
set(MINDVISION_INCLUDE_DIR "${MINDVISION_SDK_PATH}/include")
# 根据架构选择库路径
if(ARCH MATCHES "x86_64")
  set(MINDVISION_LIB_DIR "${MINDVISION_SDK_PATH}/lib/x64")
elseif(ARCH MATCHES "aarch64")
  set(MINDVISION_LIB_DIR "${MINDVISION_SDK_PATH}/lib/arm64")
else()
  message(FATAL_ERROR "Unsupported architecture for Mindvision SDK: ${ARCH}")
endif()

# --------------------------
# 组件化库编译
# --------------------------
# 收集源文件（相机实现+节点逻辑）
set(SOURCE_FILES
  src/camera_node.cpp
  src/hikrobot.cpp
  src/mindvision.cpp
  src/camera.cpp
)

# 创建共享库（组件化节点必须是共享库）
add_library(${PROJECT_NAME}_component SHARED
  ${SOURCE_FILES}
)

# 头文件路径（自定义头文件+SDK头文件）
target_include_directories(${PROJECT_NAME}_component
  PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
  ${HIKROBOT_INCLUDE_DIR}
  ${MINDVISION_INCLUDE_DIR}
  ${LIBUSB_INCLUDE_DIR} 
)

# 链接SDK库路径
target_link_directories(${PROJECT_NAME}_component
  PRIVATE
  ${HIKROBOT_LIB_DIR}
  ${MINDVISION_LIB_DIR}
)

# 链接依赖（ROS2库+SDK库）
ament_target_dependencies(${PROJECT_NAME}_component
  rclcpp
  rclcpp_components
  image_transport
  camera_info_manager
  sensor_msgs
)

# 链接具体SDK库（根据SDK文档确认库名）
target_link_libraries(${PROJECT_NAME}_component
  MvCameraControl # 海康SDK库
  MVSDK           # 迈德威视SDK库
  ${LIBUSB_LIB} 
)

# --------------------------
# 注册组件化节点
# --------------------------
# 注册插件（指定节点类的完整命名空间）
rclcpp_components_register_node(${PROJECT_NAME}_component
  PLUGIN "camera_driver::CameraNode"  # 节点类：namespace::class_name
  EXECUTABLE ${PROJECT_NAME}_node     # 生成的独立可执行文件名
)

# --------------------------
# 测试
# --------------------------
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  list(APPEND AMENT_LINT_AUTO_EXCLUDE
    ament_cmake_copyright
    ament_cmake_cpplint
    ament_cmake_uncrustify
  )
  ament_lint_auto_find_test_dependencies()

  set(_linter_excludes
    sdk/mindvision/include/CameraStatus.h
    sdk/mindvision/include/CameraDefine.h
    sdk/mindvision/include/CameraApi.h
    sdk/hikrobot/include/CameraParams.h
    sdk/hikrobot/include/MvCameraControl.h
    sdk/hikrobot/include/MvErrorDefine.h
    sdk/hikrobot/include/MvISPErrorDefine.h
    sdk/hikrobot/include/MvObsoleteInterfaces.h
    sdk/hikrobot/include/ObsoleteCamParams.h
    sdk/hikrobot/include/PixelType.h
  )
  ament_cpplint(EXCLUDE ${_linter_excludes})
endif()

# --------------------------
# 安装配置
# --------------------------
# 安装库（供动态加载）
install(TARGETS
  ${PROJECT_NAME}_component
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

# 安装头文件（如果需要被其他包依赖）
install(DIRECTORY include/
  DESTINATION include
)

# 安装配置文件（config/launch）
install(DIRECTORY
  config
  launch
  DESTINATION share/${PROJECT_NAME}
)

# 安装 SDK 文件夹
# 安装海康SDK的平台库文件到 install/lib
# （HIKROBOT_LIB_DIR已自动匹配x86_64/arm64架构）
install(DIRECTORY ${HIKROBOT_LIB_DIR}/  
  DESTINATION lib                      
  USE_SOURCE_PERMISSIONS               
  FILES_MATCHING                       
  PATTERN "*.so"                       
)

# 安装迈德威视SDK的平台库文件到 install/lib
install(DIRECTORY ${MINDVISION_LIB_DIR}/
  DESTINATION lib
  USE_SOURCE_PERMISSIONS
  FILES_MATCHING
  PATTERN "*.so"
)

ament_package()